<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <ElForm
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="文件分类" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择文件分类"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in infra_file_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文件路径" prop="path">
        <el-input
          v-model="queryParams.path"
          placeholder="请输入文件路径"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入文件类型"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="访问类型" prop="isPrivate">
        <el-select
          v-model="queryParams.isPrivate"
          placeholder="请选择访问类型"
          clearable
          class="!w-240px"
        >
          <el-option :key="0" label="公开" :value="false" />
          <el-option :key="1" label="私有" :value="true" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </ElForm>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column label="文件分类" prop="category">
        <template #default="scope">
          <dict-tag :options="infra_file_category" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column label="文件名" prop="name" show-overflow-tooltip />
      <el-table-column label="文件路径" prop="path" show-overflow-tooltip />
      <el-table-column label="文件大小" prop="size" :formatter="fileSizeFormatter" />
      <el-table-column label="文件类型" prop="type" show-overflow-tooltip />
      <el-table-column label="访问类型" prop="isPrivate">
        <template #default="{ row }">
          <el-tag v-if="row.isPrivate" type="warning" disable-transitions>私有</el-tag>
          <el-tag v-else disable-transitions>公开</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="文件内容" prop="path">
        <template #default="{ row }">
          <el-image
            v-if="isImgFile(row)"
            class="w-50px h-50px"
            lazy
            :src="s_file_url(row.path)"
            :preview-src-list="img_src_list"
            :initial-index="getImgInitialIndex(row.path)"
            preview-teleported
            hide-on-click-modal
            fit="cover"
          />
          <el-link
            v-else-if="isPdfFile(row)"
            type="primary"
            :href="s_file_url(row.path)"
            :underline="false"
            target="_blank"
            >预览
          </el-link>
          <el-link
            v-else
            type="primary"
            download
            :href="s_file_url(row.path)"
            :underline="false"
            target="_blank"
            >下载
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="上传时间" prop="createTime" width="180" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['infra:file:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import * as FileApi from '@/api/infra/file'
import { s_file_url } from '@/utils/lizard'
import { fileSizeFormatter } from '@/utils'

defineOptions({ name: 'InfraFile' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { infra_file_category } = useDict(DICT_TYPE.infra_file_category)

const queryParams = reactive({
  category: undefined,
  path: undefined,
  type: undefined,
  isPrivate: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref()

const listLoading = ref(false)

const list = ref([] as any[]) // 列表数据
const total = ref(0) // 列表数据总条数

const img_src_list = computed(() =>
  list.value.filter((item) => isImgFile(item)).map((item) => s_file_url(item.path))
)

const getImgInitialIndex = (path: string) => {
  return img_src_list.value.findIndex((url) => url.endsWith(path))
}

const isImgFile = (file: any) => {
  return file?.type?.startsWith('image')
}

const isPdfFile = (file: any) => {
  return file?.type?.includes('pdf')
}

const getList = async () => {
  listLoading.value = true
  try {
    const data = await FileApi.getFilePage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await FileApi.deleteFile(id)
    // 成功提示
    message.success(t('common.delSuccess'))
    // 刷新列表(页码不变)
    await getList()
  } catch {}
}

onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
